using System;
using System.Collections.Generic;
using System.Text;

namespace Caece.Libreria.Protocolo
{
    public enum FrontEndProtocolCommand
    {
        AreYouAlive,
        ResponseAreYouAlive,
        WhatTimeIsIt,
        ResponseWhatTimeIsIt,
        SetDeltaTime,
        ComprarItem,
        ResponseComprarItem,
        ConsultaPorItem,
        ResponseConsultaPorItem,
        ConsultaPorTema,
        ResponseConsultaPorTema,
		Votacion,
		VotacionACK
    }

	public enum CommunicationDirection
	{ 
		Send,
		Receive,
		Both
	}

    public class FrontEndProtocol
    {
        public static byte[] Encode(FrontEndProtocolCommand command, string param)
        {
            List<byte> result = new List<byte>();
            result.AddRange(BitConverter.GetBytes((int)command));
            result.AddRange(Encoding.UTF8.GetBytes(param));
            return result.ToArray();
        }

        public static FrontEndProtocolCommand Decode(byte[] data, out string param)
        {            
            FrontEndProtocolCommand command = (FrontEndProtocolCommand)BitConverter.ToInt32(data, 0);
                        
            param = Encoding.UTF8.GetString(data, 4, data.Length-4);
            
            return command;
        }

        #region Encoders
        public static string EncodeResponseWhatTimeIsIt(DateTime time)
        {
			return time.ToString();            
        }

        public static string EncodeResponseAreYouAlive()
        {
            return "YES";
        }

        public static string EncodeResponseComprarItem(bool param)
        {
            return param.ToString();
        }
        #endregion

        #region Decoders
        public static DateTime DecodeTheTimeIs(string param)
        {
            return DateTime.Parse(param);
        }

        public static TimeSpan DecodeSetDeltaTime(string param)
        {
			return TimeSpan.Parse(param.Replace("\0", ""));
        }

        public static Int32 DecodeComprarItem(string param)
        {
            return Int32.Parse(param);
        }

        public static Int32 DecodeConsultaPorItem(string param)
        {
            return Int32.Parse(param);
        }

        public static String DecodeConsultaPorTema(string param)
        {
			return param.Replace("\0", "").Trim();
        }

		public static bool DecodeVotacionACK(string param)
		{
			return Boolean.Parse(param);
		}
        #endregion

    }

}
